package com.joker.demo.admin.util;

import com.alibaba.fastjson2.JSON;
import com.joker.demo.admin.domain.po.ThreadPoolMonitorPO;
import com.joker.demo.core.domain.dto.ThreadPoolMonitorDTO;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * 线程池监控数据转换工具类
 *
 * @author : feixiang.li
 * @since : 2025-01-27 10:00
 */
public class ThreadPoolMonitorConvertUtils {

    /**
     * 将ThreadPoolMonitorDTO转换为ThreadPoolMonitorPO
     *
     * @param dto 监控数据传输对象
     * @return 监控数据持久化对象
     */
    public static ThreadPoolMonitorPO convertToPO(ThreadPoolMonitorDTO dto) {
        if (dto == null) {
            return null;
        }

        ThreadPoolMonitorPO po = new ThreadPoolMonitorPO();
        
        // 基本信息
        po.setRegisterKey(dto.getRegisterKey());
        po.setAppName(dto.getAppName());
        po.setMonitorTime(LocalDateTime.ofInstant(
            new Date(dto.getTimestamp()).toInstant(), 
            ZoneId.systemDefault()
        ));
        po.setTimestamp(dto.getTimestamp());

        // 线程池基本信息
        if (dto.getThreadPoolInfo() != null) {
            ThreadPoolMonitorDTO.ThreadPoolInfo threadPoolInfo = dto.getThreadPoolInfo();
            po.setCorePoolSize(threadPoolInfo.getCorePoolSize());
            po.setMaximumPoolSize(threadPoolInfo.getMaximumPoolSize());
            po.setPoolSize(threadPoolInfo.getPoolSize());
            po.setActiveCount(threadPoolInfo.getActiveCount());
            po.setQueueSize(threadPoolInfo.getQueueSize());
            po.setQueueRemainingCapacity(threadPoolInfo.getQueueRemainingCapacity());
            po.setCompletedTaskCount(threadPoolInfo.getCompletedTaskCount());
            po.setTaskCount(threadPoolInfo.getTaskCount());
        }

        // 内存使用信息
        if (dto.getMemoryInfo() != null) {
            ThreadPoolMonitorDTO.MemoryInfo memoryInfo = dto.getMemoryInfo();
            po.setMaxMemoryMb(memoryInfo.getMaxMemoryMB());
            po.setTotalMemoryMb(memoryInfo.getTotalMemoryMB());
            po.setUsedMemoryMb(memoryInfo.getUsedMemoryMB());
            po.setFreeMemoryMb(memoryInfo.getFreeMemoryMB());
            po.setMemoryUsagePercent(BigDecimal.valueOf(memoryInfo.getMemoryUsagePercent()));
        }

        // 任务执行统计
        if (dto.getTaskStatistics() != null) {
            ThreadPoolMonitorDTO.TaskStatistics taskStatistics = dto.getTaskStatistics();
            po.setRunningTaskCount(taskStatistics.getRunningTaskCount());
            po.setDelayTaskCount(taskStatistics.getDelayTaskCount());
            po.setCompletedTasksLastMinute(taskStatistics.getCompletedTasksLastMinute());
            po.setFailedTasksLastMinute(taskStatistics.getFailedTasksLastMinute());
            po.setAverageTaskExecutionTimeMs(BigDecimal.valueOf(taskStatistics.getAverageTaskExecutionTimeMs()));
        }

        // 线程堆栈信息（JSON格式存储）
        if (dto.getThreadStackInfos() != null && !dto.getThreadStackInfos().isEmpty()) {
            po.setThreadStackInfo(JSON.toJSONString(dto.getThreadStackInfos()));
        }

        return po;
    }

    /**
     * 将ThreadPoolMonitorPO转换为ThreadPoolMonitorDTO
     *
     * @param po 监控数据持久化对象
     * @return 监控数据传输对象
     */
    public static ThreadPoolMonitorDTO convertToDTO(ThreadPoolMonitorPO po) {
        if (po == null) {
            return null;
        }

        ThreadPoolMonitorDTO dto = new ThreadPoolMonitorDTO();
        
        // 基本信息
        dto.setRegisterKey(po.getRegisterKey());
        dto.setAppName(po.getAppName());
        dto.setTimestamp(po.getTimestamp());

        // 线程池基本信息
        ThreadPoolMonitorDTO.ThreadPoolInfo threadPoolInfo = new ThreadPoolMonitorDTO.ThreadPoolInfo();
        threadPoolInfo.setCorePoolSize(po.getCorePoolSize());
        threadPoolInfo.setMaximumPoolSize(po.getMaximumPoolSize());
        threadPoolInfo.setPoolSize(po.getPoolSize());
        threadPoolInfo.setActiveCount(po.getActiveCount());
        threadPoolInfo.setQueueSize(po.getQueueSize());
        threadPoolInfo.setQueueRemainingCapacity(po.getQueueRemainingCapacity());
        threadPoolInfo.setCompletedTaskCount(po.getCompletedTaskCount());
        threadPoolInfo.setTaskCount(po.getTaskCount());
        dto.setThreadPoolInfo(threadPoolInfo);

        // 内存使用信息
        ThreadPoolMonitorDTO.MemoryInfo memoryInfo = new ThreadPoolMonitorDTO.MemoryInfo();
        memoryInfo.setMaxMemoryMB(po.getMaxMemoryMb());
        memoryInfo.setTotalMemoryMB(po.getTotalMemoryMb());
        memoryInfo.setUsedMemoryMB(po.getUsedMemoryMb());
        memoryInfo.setFreeMemoryMB(po.getFreeMemoryMb());
                    memoryInfo.setMemoryUsagePercent(po.getMemoryUsagePercent().doubleValue());
        dto.setMemoryInfo(memoryInfo);

        // 任务执行统计
        ThreadPoolMonitorDTO.TaskStatistics taskStatistics = new ThreadPoolMonitorDTO.TaskStatistics();
        taskStatistics.setRunningTaskCount(po.getRunningTaskCount());
        taskStatistics.setDelayTaskCount(po.getDelayTaskCount());
        taskStatistics.setCompletedTasksLastMinute(po.getCompletedTasksLastMinute());
        taskStatistics.setFailedTasksLastMinute(po.getFailedTasksLastMinute());
                    taskStatistics.setAverageTaskExecutionTimeMs(po.getAverageTaskExecutionTimeMs().doubleValue());
        dto.setTaskStatistics(taskStatistics);

        // 线程堆栈信息（JSON格式解析）
        if (po.getThreadStackInfo() != null && !po.getThreadStackInfo().trim().isEmpty()) {
            try {
                dto.setThreadStackInfos(JSON.parseArray(po.getThreadStackInfo(), ThreadPoolMonitorDTO.ThreadStackInfo.class));
            } catch (Exception e) {
                // 解析失败时忽略
            }
        }

        return dto;
    }
}
